<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1.实现随机选取10~100之间的10个数字，存入一个数组，用冒泡排序， 并输出每一轮的结果。
        let randomNum = [];
        for (i = 0; i < 10; i++) {
            n = parseInt(Math.random() * 91) + 10
            randomNum.push(n)
        }
        console.log(`初始随机数组：`, randomNum)

        let cycleNum = 0;
        for (j = 0; j < randomNum.length - 1; j++) {
            for (i = 0; i < randomNum.length - 1 - j; i++) {
                cycleNum++;
                let n = 0;
                if (randomNum[i] > randomNum[i + 1]) {
                    n = randomNum[i + 1];
                    randomNum[i + 1] = randomNum[i];
                    randomNum[i] = n;
                }
                // console.log(`内层循环：`, randomNum)
            }
            // console.log(`外层循环：`, randomNum)
        }
        console.log(`冒泡排序结果：`, randomNum)
        console.log(`循环次数：`, cycleNum)

        // 2.实现随机选取10~100之间的10个数字，存入一个数组，用选择排序， 并输出每一轮的最小索引
        let secondRandomNum = [];
        for (i = 0; i < 10; i++) {
            n = parseInt(Math.random() * 91) + 10
            secondRandomNum.push(n)
        }
        console.log(`初始随机数组：`, secondRandomNum)

        let secondCycleNum = 0;
        let assNum = 0

        for (j = 0; j < secondRandomNum.length - 1; j++) {
            let minNum = j; //假定最小值索引
            
            for (let i = j + 1; i < secondRandomNum.length; i++) {
                secondCycleNum++;
                if (secondRandomNum[minNum] > secondRandomNum[i]) {
                    minNum = i;
                }
            }
            assNum++;
            console.log(`第${j}轮最小索引：`, minNum)

            let m = secondRandomNum[minNum];
            secondRandomNum[minNum] = secondRandomNum[j];
            secondRandomNum[j] = m;
        }
        

        console.log(`选择排序结果：`, secondRandomNum)
        console.log(`循环次数：`, secondCycleNum)
        console.log(`赋值次数：`, assNum)
        
        

    </script>
</body>
</html>